package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.ConexaoBD;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import model.Experiencia;

public class DAOExperiencia extends ModelDAO<Experiencia>{
	private DAOAluno daoAluno = new DAOAluno();
	
	public DAOExperiencia() {
        super(Experiencia.class);
    }
	
	public void save(Experiencia ex){
		super.begin();
		super.save(ex);
		super.commit();
	}
	
	public void update(Experiencia ex){
		super.begin();
		super.update(ex);
		super.commit();
	}
	
	public Experiencia search(int cod){
		super.begin();
		Experiencia ex = super.search(cod);
		super.commit();
		return ex;
	}
	
	public List<Experiencia> listar(){
		super.begin();
        List<Experiencia> list = super.findAll();
        super.commit();
        return list;
    }
	
	public void delete(Experiencia e){
		super.begin();
		super.delete(e.getCodExp());
		super.commit();
	}
	public List<Experiencia> listar(Integer codAluno) throws SQLException{
		// conectar banco
		Connection con = ConexaoBD.getConnection();
		PreparedStatement stmt = (PreparedStatement) con.prepareStatement ("select * from experiencia, aluno where aluno.codAluno="+codAluno+" and experiencia.codAluno=aluno.codAluno");
		ResultSet rs = stmt.executeQuery();
			
		List<Experiencia> experiencia = new ArrayList<Experiencia>();
			
		while (rs.next()){
		//criando o objeto usuario
		Experiencia exp = new Experiencia();
		exp.setCodExp(rs.getInt("codExp"));
		exp.setCargoExp(rs.getString("cargoExp"));
		exp.setEmpresaExp(rs.getString("empresaExp"));
		exp.setDtAdmExp(rs.getDate("dtAdmExp"));
		exp.setDtDemEmp(rs.getDate("dtDemEmp"));
		exp.setTelExp(rs.getString("telExp"));
		exp.setContatoExp(rs.getString("contatoExp"));
		exp.setCidadeExp(rs.getString("cidadeExp"));
		exp.setUfExp(rs.getString("ufExp"));
		Integer codAl = rs.getInt("codAluno");
		exp.setAluno(daoAluno.search(codAl));
		
		//adicionando o objeto a lista
		experiencia.add(exp);
		}
		
		//}
		return experiencia;
	}
}
